<?php
class AccountsController extends AppController {
    /** @var Account */
    var $Account;

    function index() {
        $accounts = $this->Account->find('all');
        if (empty($accounts)) {
            $this->Session->setFlash('There are no accounts defined', 'default', array('class' => 'bad'));
        } else {
            $this->set('accounts', $accounts);
        }
    }

    function view($id) {
        $account = $this->Account->findById($id);
        if (!empty($account)) {
            $this->set('account', $account);
        } else {
            //TODO# CSS
            $this->Session->setFlash('Invalid Account ID', 'default', array('class' => 'bad'));
            $this->redirect('index');
        }
    }

    function add() {
        if (!empty($this->data)) {
            // here, $this->data['Account']['password'] is already hashed so we need
            // to hash confirm password to validate password properly
            $this->data['Account']['confirm_password'] = $this->Auth->password($this->data['Account']['confirm_password']);
            $this->Account->set($this->data);
            if ($this->Account->validates()) {
                $this->Account->save($this->data, false);
                $this->Session->setFlash($this->data['Account']['email'] . ' Added', 'default', array('class' => 'good'));
                $this->redirect('index');
            } else {
                $this->data['Account']['password'] = "";
                $this->data['Account']['confirm_password'] = "";
                $this->Session->setFlash('Please correct the bads below', 'default', array('class' => 'bad'));
            }
        }
    }

    function edit($id = null) {
        if (!empty($this->data)) {
            // here, $this->data['Account']['password'] is already hashed so we need
            // to hash confirm password to validate password properly
            $this->data['Account']['confirm_password'] = $this->Auth->password($this->data['Account']['confirm_password']);
            $this->Account->set($this->data);
            if ($this->Account->validates()) {
                $this->Account->save($this->data, false);
                $this->Session->setFlash($this->data['Account']['email'].' Updated', 'default', array('class'=>'good'));
                $this->redirect('index');
            }
        } else {
            $account = $this->Account->findById($id);
            if (empty($account)) {
                $this->Session->setFlash('Invalid Account ID', 'default', array('class'=>'bad'));
                $this->redirect('index');
            } else {
                unset($account['Account']['password']);
                $this->data = $account;
            }
        }
    }

    function delete($id) {
        $account = $this->Account->findById($id);
        if (empty($account)) {
            $this->Session->setFlash('Invalid Account ID', 'default', array('class' => 'bad'));
        } else {
            if ($account['Account']['id'] == $this->Session->read('Auth.Account.id')) {
                $this->Session->setFlash('Sorry, you can not delete yourself', 'default', array('class' => 'bad'));
            } else {
                if ($this->Account->delete($id)) {
                    $this->Session->setFlash($account['Account']['email'] . ' Deleted', 'default', array('class' => 'good'));
                } else {
                    $this->Session->setFlash('Failed to delete ' . $account['Account']['email'], 'default', array('class' => 'bad'));
                }
            }
        }
        $this->redirect('index');
    }

    function login() {
        if($this->Auth->user() != null) {
            $this->redirect("welcome");
        }
    }

    function logout() {
        $this->Session->delete('Auth.Account');
        $this->Session->setFlash('Your are now logged out', 'default', array('class' => 'bad'));
    }

    function validateLogin() {
        $this->Auth->login($this->data);
        $this->set('account', $this->Auth->user());
    }

    function welcome() {
        $this->set('data', $this->Auth->user());
    }

}
?>